import {stringify} from 'qs';
import request from '../utils/request';
import * as menuConfig from './../common/menu';
import {getToken} from "../utils/authority";


export async function queryLog(params) {
  return request(`/admin/log/logPage?${stringify(params)}`, {
    headers: {
      'Authorization': `Bearer ${getToken()}`,
    },
  });
}

export async function queryProjectNotice() {
  return request('/api/project/notice');
}

export async function queryActivities() {
  return request('/api/activities');
}

export async function queryRule(params) {
  return request(`/api/rule?${stringify(params)}`);
}

export async function removeRule(params) {
  return request('/api/rule', {
    method: 'POST',
    body: {
      ...params,
      method: 'delete',
    },
  });
}

export async function addRule(params) {
  return request('/api/rule', {
    method: 'POST',
    body: {
      ...params,
      method: 'post',
    },
  });
}

export async function fakeSubmitForm(params) {
  return request('/api/forms', {
    method: 'POST',
    body: params,
  });
}

export async function fakeChartData() {
  return request('/api/fake_chart_data');
}

export async function queryTags() {
  return request('/api/tags');
}

export async function queryBasicProfile() {
  return request('/api/profile/basic');
}

export async function queryAdvancedProfile() {
  return request('/api/profile/advanced');
}

export async function queryFakeList(params) {
  return request(`/api/fake_list?${stringify(params)}`);
}


export async function queryNotices() {
  return request('/api/notices');
}

/* 自定义 */
export async function getAuthMenus() {
  return request('/admin/menu/userMenu',
    {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${getToken()}`,
      },
    }
  );

  // return new Promise(function (resolve) {
  //   console.log(menuData)
  //   resolve(menuConfig.getMenuData(menuData));
  // });
}

export async function getRouterData(routerConfig, menuData) {
  const flatMenuData = menuConfig.getFlatMenuData(menuData);
  return menuConfig.getRouterData(routerConfig, flatMenuData);
}

export function getRouterDataSync(routerConfig, menuData) {
  const flatMenuData = menuConfig.getFlatMenuData(menuData);
  return menuConfig.getRouterData(routerConfig, flatMenuData);
}

export async function getUsers(params) {
  // return request(`/api/admin/users?${stringify(params)}`);
  return request(`/admin/user/userPage?${stringify(params)}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function getRoles(params) {
  // return request(`/api/admin/roles?${stringify(params)}`);
  return request(`/admin/role/rolePage?${stringify(params)}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function getRolesByDept(params) {
  // return request(`/api/admin/roles?${stringify(params)}`);
  return request(`/admin/role/roleList/${params.deptId}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function addRoles(params) {
  return request('/admin/role/', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
    body: {
      ...params,
    },
  });
}

export async function delRoles(params) {
  return request(`/admin/role/${params.roleId}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function updRoles(params) {
  return request('/admin/role/', {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
    body: {
      ...params,
    },
  });
}

export async function getDeptList(params) {
  return request('/admin/dept/tree', {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
    body: {
      ...params,
    },
  });
}

export async function getDeptIds(params) {
  return request(`/admin/dept/tree/${params.deptId}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function getAllMenuTree() {
  return request('/admin/menu/allTree/', {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function getRoleMenuTree(params) {
  return request(`/admin/menu/findMenuByRole/${params.roleCode}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function updRoleMenu(params) {
  return request(`/admin/role/roleMenuUpd?${stringify(params, {arrayFormat: 'brackets'})}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  });
}

export async function addUser(params) {
  return request('/admin/user/', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
    body: {
      ...params,
    },
  });
}

export async function updUser(params) {
  return request('/admin/user/', {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
    body: {
      ...params,
    },
  });
}

export async function delUser(params) {
  return request(`/admin/user/${params.userId}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${getToken()}`,
    },
  })
}
